iT邦幫忙

2024 iThome 鐵人賽

0
IT 管理

API Gateway:微服務世界的守護者系列 第 25

Day 25 APISIX | Load Balancing

  • 分享至 

  • xImage
  •  

(Draft) Load Balancing tutorial

https://ithelp.ithome.com.tw/upload/images/20250304/201115164m41YQDWPo.png
APISIX 如何扮演 Load Balancer 的角色。

Description

  • 負載平衡(Load Balancing)管理client與server之間的流量。
  • 負載平衡是決定哪個server處理特定request的機制,以達到優化效能,可擴展性,可靠性。
  • 負載平衡適用於設計處理大量流量的系統。

Configuration

Enable Load Balancing

建立具有兩個上游服務的路由。

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
  "id": "getting-started-headers",
  "uri": "/headers",
  "upstream" : {
    "type": "roundrobin",
    "nodes": {
      "httpbin.org:443": 1,
      "mock.api7.ai:443": 1
    },
    "pass_host": "node",
    "scheme": "https"
  }
}'

成功建立Route回傳HTTP/1.1 201 Created

Validate

如何驗證負載平衡的結果?
上游服務回傳不同資料回來。

  • upstream#1 httpbin.org
    return headers.host-httpbin.org
  • upstream#2 mock.api7.ai
    return headers.host-mock.api7.ai

產生100次Requests來測試負載平衡的效果。

hc=$(seq 100 | xargs -I {} curl "http://127.0.0.1:9080/headers" -sL | grep "httpbin" | wc -l); 
echo httpbin.org: $hc, 
mock.api7.ai: $((100 - $hc))

結果顯示:
httpbin.org: 51, mock.api7.ai: 49
Requests打到上游服務的次數接近相等。

Reference

apisix as lb

Thinking...

When type is roundrobin and weight is not equal,

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
  "id": "getting-started-headers",
  "uri": "/headers",
  "upstream" : {
    "type": "roundrobin",
    "nodes": {
      "httpbin.org:443": 1,
      "mock.api7.ai:443": 2
    },
    "pass_host": "node",
    "scheme": "https"
  }
}'

What is the result ?


上一篇
Day 24 - K8s APISIX Autoscale 設定
下一篇
Day 26 Note: OSS - apisix
系列文
API Gateway:微服務世界的守護者30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言